/**
 * Created by jiangguoshun on 16/7/6.
 */

$(function() {
	// 目录中集数的鼠标悬浮事件
	$(
			'.main-content .audio-main-wrapper .audio-tool-bar-wrapper .catalog-extra .content .items .item')
			.hover(function() {
				var _this = $(this);
				_this.closest('.items').find('.item .item-desc').hide();
				_this.find('.item-desc').show();
			}, function() {

			});

	// 通过目录按钮打开关闭目录侧边栏
	$('.audio-tool-bar-main .catalog')
			.click(
					function() {
						// 还原展开当前的集数介绍
						$(
								'.main-content .audio-main-wrapper .audio-tool-bar-wrapper .catalog-extra .content .items .item')
								.each(
										function(inx, val) {
											var _this_val = $(val);
											if (_this_val
													.find(
															'.item-title .item-title-left')
													.hasClass('cur-item')) {
												_this_val.find('.item-desc')
														.show();
											} else {
												_this_val.find('.item-desc')
														.hide();
											}
										});

						var catalogExtra = $('.audio-tool-bar-wrapper').find(
								'.' + $(this).data('extra'));
						if (catalogExtra.is(":hidden")) {
							catalogExtra.slideLeftShow();
							$(this).find('i').removeClass('undertone')
									.addClass('thick');
						} else {
							catalogExtra.slideLeftHide();
							$(this).find('i').removeClass('thick').addClass(
									'undertone');
						}
					});
	// 收起目录侧边栏
	$('.audio-tool-bar-wrapper .catalog-extra .content .title .title-right')
			.click(function() {
				$('.audio-tool-bar-main .catalog').trigger('click');
			});

	// 监控视频侧边栏的鼠标悬停事件
	$('.audio-tool-bar-main .audio-bar').hover(
			function() {
				var _this = $(this);
				if (_this.hasClass('share') || _this.hasClass('download')
						|| _this.hasClass('comment')) {
					_this.find('i').removeClass('undertone').addClass('thick');
				}
				if (_this.hasClass('catalog')) { // 目录不处理
					return;
				}
				$('.audio-tool-bar-wrapper').find('.' + $(this).data('extra'))
						.slideLeftShow();
			},
			function() {
				var _this = $(this);

				if (_this.hasClass('share') || _this.hasClass('download')
						|| _this.hasClass('comment')) {
					_this.find('i').removeClass('thick').addClass('undertone');
				}
				if (_this.hasClass('catalog')) {
					return;
				}
				$('.audio-tool-bar-wrapper').find('.' + $(this).data('extra'))
						.slideLeftHide();
			});

	// 分享侧边栏有动画效果 特殊处理
	$('.audio-tool-bar-wrapper .share-extra').hover(function() {
		$(this).stop().show();
	}, function() {
		$(this).slideLeftHide();
	});

	// 评论侧边被点击 跳到评论位置
	$('.audio-tool-bar-main .comment')
			.click(
					function() {
						$("html,body")
								.animate(
										{
											scrollTop : $(
													'.audio-main-info-wrapper .audio-others-wrapper')
													.offset().top
										}, 500);
						$('.audio-others-wrapper .tabs .comment-tab').trigger(
								'click');
					});
	// 点赞侧边栏点击
	$('.audio-tool-bar-main .like').click(function() {
		var _this = $(this);

		var attitudeVals = _this.find('.value-store').val().split(',');
		var data = {
			itemType : attitudeVals[0],
			itemId : attitudeVals[1],
			type : attitudeVals[2]
		};

		var _this_i = _this.find('i');
		var _this_span = _this.find('span');
		if (_this_i.hasClass('undertone')) {
			dealAttitude(data, "add", _this_i, _this_span);
		} else {
			dealAttitude(data, "cancle", _this_i, _this_span);
		}
	});

	// 弹幕侧边栏点击
	$('.audio-tool-bar-main .barrage').click(function() {
		var _this_i = $(this).find('i');
		if (_this_i.hasClass('undertone')) {
			_this_i.removeClass('undertone').addClass('thick');
		} else {
			_this_i.removeClass('thick').addClass('undertone');
		}
	});

	// 收藏侧边栏 有已收藏和为收藏两种状态 状态切换
	$('.audio-tool-bar-main .collection')
			.click(
					function() {
						var _this = $(this);
						var _anohter_this = $('.audio-info-wrapper .audio-info .bar .collection-bar');
						var _anohter_this_i = _anohter_this.find('i');
						var _this_i = _this.find('i');

						var vals = _this.find('.value-store').val().split(',');
						var data = {
							itemType : vals[0],
							itemId : vals[1],
							favoriteId : vals[2],
							id : vals[3]
						};

						if (_this_i.hasClass('undertone')) {
							dealFavorite(data, 'add', _this_i, _anohter_this_i);
						} else {
							dealFavorite(data, 'cancle', _this_i,
									_anohter_this_i);
						}
					});

	// 收藏按钮点击事件
	$('.audio-info-wrapper .audio-info .bar .collection-bar').click(function() {
		var _this = $(this);
		var _anohter_this = $('.audio-tool-bar-main .collection');
		var _anohter_this_i = _anohter_this.find('i');
		var _this_i = _this.find('i');

		var vals = _this.find('.value-store').val().split(',');
		var data = {
			itemType : vals[0],
			itemId : vals[1],
			favoriteId : vals[2],
			id : vals[3]
		};

		if (_this_i.hasClass('undertone')) {
			dealFavorite(data, 'add', _this_i, _anohter_this_i);
		} else {
			dealFavorite(data, 'cancle', _this_i, _anohter_this_i);
		}
	});

	// 下载按钮
	$('.audio-info-wrapper .audio-info .bar .download-bar').hover(function() {
		var _this = $(this);
		_this.find('i').removeClass('undertone').addClass('thick');
	}, function() {
		var _this = $(this);
		_this.find('i').removeClass('thick').addClass('undertone');
	});

	// 状态切换时改变文字颜色
	$('.audio-tool-bar-main .play-continuously,.turn-off-the-light').click(
			function() {
				var _this_i = $(this).find('i');
				if (_this_i.hasClass('undertone')) {
					_this_i.removeClass('undertone').addClass('thick');
				} else {
					_this_i.removeClass('thick').addClass('undertone');
				}
			});

	// tab标签切换
	$('.audio-others-wrapper .tabs .tab').click(
			function() {
				var _this = $(this);
				if (!_this.hasClass('tab-cur')) {
					$('.audio-others-wrapper .tabs').find('.tab').each(
							function(inx, val) {
								$(
										'.audio-others-wrapper .'
												+ $(val).data('tab')).hide();
								$(val).removeClass('tab-cur');

							});
					_this.addClass('tab-cur');
					$('.audio-others-wrapper .' + _this.data('tab')).show();

				}
			});
	// 评论类型tab切换
	$('.audio-others-wrapper .audio-comments .mods-wrapper .mods .mod a')
			.click(
					function() {

						var _this = $(this);
						var _this_mod = _this.closest('.mod');
						if (!_this_mod.hasClass('mod-cur')) {
							_this_mod.closest('.mods').find('.mod').each(
									function(inx, val) {
										$(val).removeClass('mod-cur').find(
												'.mod-span').removeClass(
												'mod-span-cur');
									});
							_this_mod.addClass('mod-cur').find('.mod-span')
									.addClass('mod-span-cur');

							var typeStr = _this.data('extral');
							var type = 0;

							if (typeStr == 'all-items') {
								type = 0;
							} else if (typeStr == 'hot-items') {
								type = 1;
							} else if (typeStr == 'authentication-items') {
								type = 2;
							} else if (typeStr == 'attention-items') {
								type = 3;
							} else if (typeStr == 'stranger-items') {
								type = 4;
							}

							getCommentList(type, 1, 2, audioId);

							var modWrapper = $('.audio-others-wrapper .audio-comments .mods-wrapper .mod-content-wrapper');
							modWrapper.find('.item-type').hide();

							modWrapper.find('.' + typeStr).show();

						}
					});
});

// 处理文字溢出
$(function() {
	var this_audio_desc = $('.audio-tool-bar-wrapper .catalog-extra .content>.desc');
	// 1 设置所需的长度 字节
	this_audio_desc.attr("displayLength", "350");
	// 2 调用该插件
	// 方式一 溢出与还原切换
	// this_audio_desc.un_displayPart();
	// 方式二 溢出不还原
	this_audio_desc.text_overflow();

	// 处理文字溢出
	var this_item_desc = $('.audio-tool-bar-wrapper .catalog-extra .content .items .item .item-desc');
	this_item_desc.attr("displayLength", "300");
	this_item_desc.text_overflow();

});

$(function() {
	// 删除评论监控事件
	$(
			'.mod-content-wrapper .mod-content-item .comment-info .comment-bars .bars .delete-bar')
			.click(
					function() {
						var _this = $(this);
						var _this_mod_content_item = _this
								.closest('.mod-content-item');

						var vals = _this.find('.value-store').val().split(',');
						var data = {
							id : vals[0]
						};

						deleteComment(data, _this_mod_content_item);
					});

	// 监控点击回复事件
	$(
			'.mod-content-wrapper .mod-content-item .comment-info .comment-bars .bars .reply-bar')
			.click(
					function() {
						var _this = $(this);
						var _this_comment_info = _this.closest('.comment-info');
						var _this_arrow = _this_comment_info.find(
								'.comment-bars').find('.arrow');
						if (!_this_arrow.is(":hidden")) { // 不是隐藏的
							_this_arrow.hide();
							_this_comment_info.find('.reply').hide();
						} else {
							_this_arrow.show();
							_this_comment_info.find('.reply').show();
						}

					});

	// 监控点赞事件
	$(
			'.mod-content-wrapper .mod-content-item .comment-info .comment-bars .bars .like-bar')
			.click(function() {
				var _this = $(this);

				var attitudeVals = _this.find('.value-store').val().split(',');
				var data = {
					itemType : attitudeVals[0],
					itemId : attitudeVals[1],
					type : attitudeVals[2]
				};

				var _this_i = _this.find('i');
				var _this_span = _this.find('span');
				if (_this_i.hasClass('undertone')) {
					dealAttitude(data, "add", _this_i, _this_span);
				} else {
					dealAttitude(data, "cancle", _this_i, _this_span);
				}

			});

	// 举报按钮隐藏展示事件
	$(
			'.mod-content-wrapper .mod-content-item .comment-info .comment-bars .bars')
			.hover(function() {
				var _this = $(this);
				_this.find('.report-bar').css('visibility', 'visible');
				_this.find('.report-bar-split').css('visibility', 'visible');
			}, function() {
				var _this = $(this);
				_this.find('.report-bar').css('visibility', 'hidden');
				_this.find('.report-bar-split').css('visibility', 'hidden');
			});

	layer.config({
		extend : [ '../layer-v2.4/skin/moon/style.css' ], // 加载新皮肤
		skin : 'layer-ext-moon'
	});

});

/**
 * 收藏增加取消接口
 * 
 * @param favoriteData
 *            收藏数据
 * @param optype
 *            操作 add/cancle
 * @param _this_i
 *            当前收藏图标
 * @param _anohter_this_i
 *            另一个收藏图标
 */
function dealFavorite(favoriteData, optype, _this_i, _anohter_this_i) {
	$.ajax({
		// async : false,
		type : "POST",
		data : favoriteData,
		url : baseUrl + "/favorite/" + optype,
		success : function(responseData) {
			if (responseData.responseCode == '000') {
				if (optype == 'add') {
					_this_i.removeClass('undertone');
					_this_i.addClass('thick');

					_this_i.parent().find('.value-store').val(
							responseData.data.itemType + ","
									+ responseData.data.itemId + ","
									+ responseData.data.favoriteId + ","
									+ responseData.data.id);

					if (_anohter_this_i) {
						_anohter_this_i.removeClass('undertone');
						_anohter_this_i.addClass('thick');

						_anohter_this_i.parent().find('.value-store').val(
								responseData.data.itemType + ","
										+ responseData.data.itemId + ","
										+ responseData.data.favoriteId + ","
										+ responseData.data.id);
					}

				} else {

					_this_i.removeClass('thick');
					_this_i.addClass('undertone');

					if (_anohter_this_i) {
						_anohter_this_i.removeClass('thick');
						_anohter_this_i.addClass('undertone');
					}
				}

			} else {
				var options = {
					type : SELF_ALERT_TYPES.danger,
					msg : responseData.responseMessage,
					hasClose : true,
					time : 3000
				};
				selfAlert(options);
			}

		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			var msg = XMLHttpRequest.statusText;
			var options = {
				type : SELF_ALERT_TYPES.danger,
				msg : msg,
				hasClose : true,
				time : 3000
			};
			selfAlert(options);
		}
	});

}

/**
 * 态度增加取消接口
 * 
 * @param attitudeData
 *            态度数据
 * @param optype
 *            操作类型 add/cancle
 * @param _this_i
 *            图标对象
 * @param _this_span
 *            态度总数展示区对象
 */
function dealAttitude(attitudeData, optype, _this_i, _this_span) {
	$
			.ajax({
				// async : false,
				type : "POST",
				data : attitudeData,
				url : baseUrl + "/attitude/" + optype,
				success : function(responseData) {
					if (responseData.responseCode == '000') {
						if (optype == 'add') {
							_this_i.removeClass('undertone').addClass('thick');
						} else {
							_this_i.removeClass('thick').addClass('undertone');
						}
						if (attitudeData.type == 1) {
							if (responseData.data.statistics.countPraise < 1) {
								_this_span.html('');
							} else {
								_this_span
										.html(responseData.data.statistics.showCountPraise);
							}

						} else {
							// TODO 可以继续增加
						}

					} else {
						var options = {
							type : SELF_ALERT_TYPES.danger,
							msg : responseData.responseMessage,
							hasClose : true,
							time : 3000
						};
						selfAlert(options);
					}

				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					var msg = XMLHttpRequest.statusText;
					var options = {
						type : SELF_ALERT_TYPES.danger,
						msg : msg,
						hasClose : true,
						time : 3000
					};
					selfAlert(options);
				}
			});

}

/**
 * 评论监控
 */
$(function() {
	$('.audio-others-wrapper .audio-comments .comment-btn-div')
			.click(
					function() {
						var allItem = $('.audio-others-wrapper .audio-comments .mods-wrapper .mod-content-wrapper .all-items .mod-content-items');// 默认添加到全部中

						var _this = $(this);
						var content = $.trim(_this.closest('.audio-comments')
								.find('.form-group textarea').val());

						var datsStrs = _this.find('.value-store').val().split(
								',');
						var data = {
							itemType : datsStrs[0],
							itemId : datsStrs[1],
							content : content
						};
						addComment(data, allItem);

					});

	/**
	 * 回复中的评论监控
	 */
	$(
			'.audio-others-wrapper .audio-comments .mods-wrapper .mod-content-items .mod-content-item .comment-info .reply .reply-btn-div')
			.click(
					function() {
						var _this = $(this);
						var mod_content_items = _this
								.closest('.mod-content-items');

						String
						content = $.trim(_this.closest('.reply').find(
								'.form-group textarea').val());

						var datsStrs = _this.find('.value-store').val().split(
								',');
						var data = {
							itemType : datsStrs[0],
							itemId : datsStrs[1],
							content : content
						};
						addComment(data, mod_content_items);
					});
});

/**
 * 添加评论
 * 
 * @param data
 */
function addComment(data, _add_to_elem, _from_type) {
	var options = {
		type : '',
		msg : "",
		hasClose : true,
		time : 3000
	};

	if (!data.content) {
		options.type = SELF_ALERT_TYPES.warning;
		options.msg = '请填写评论内容';
		selfAlert(options);
		return;
	} else if (data.content.length > 200) {
		options.type = SELF_ALERT_TYPES.warning;
		options.msg = '评论内容在200字以内';
		selfAlert(options);
		return;
	}

	$
			.ajax({
				async : false,
				type : "POST",
				data : data,
				url : baseUrl + "/comment/add",
				success : function(responseData) {
					if (responseData.responseCode == '000') {
						var _options = {
							type : SELF_ALERT_TYPES.success,
							msg : "评论成功",
							hasClose : true,
							time : 3000
						};
						selfAlert(_options);
						if (_add_to_elem) {
							var _url_part = '/comment/get/';
							if (_from_type) {
								if (_from_type == 'dialog') {
									_url_part = '/comment/dialog/get/';
								}
							}
							$
									.ajax({
										// async : false,
										type : "POST",
										data : data,
										url : baseUrl + _url_part
												+ responseData.data.id
												+ ".html",
										success : function(html) {
											_add_to_elem.prepend(html);
											if (_from_type) {
												if (_from_type == 'dialog') {
													$('.time-item-dyc-add')
															.prependTo(
																	$('.show-dialog-module-wrapper .show-dialog-wrapper .dialog-time-wrapper .time-items'))
															.removeClass(
																	'time-item-dyc-add');
													_RESET_SHOW_DIALOG_MODULE_WRAPPER_FUN_();
												}
											}
										},
										error : function(XMLHttpRequest,
												textStatus, errorThrown) {
											var msg = XMLHttpRequest.statusText;
											var _options = {
												type : SELF_ALERT_TYPES.danger,
												msg : msg,
												hasClose : true,
												time : 3000
											};
											selfAlert(_options);
										}
									});
						}
					} else {
						var _options = {
							type : SELF_ALERT_TYPES.danger,
							msg : responseData.responseMessage,
							hasClose : true,
							time : 3000
						};
						selfAlert(_options);
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					var msg = XMLHttpRequest.statusText;
					var _options = {
						type : SELF_ALERT_TYPES.danger,
						msg : msg,
						hasClose : true,
						time : 3000
					};
					selfAlert(_options);
				}
			});
}

/**
 * 获取评论列表
 * 
 * @param type
 * @param index
 * @param itemType
 * @param itemId
 */
function getCommentList(type, index, itemType, itemId) {

	$
			.ajax({
				// async : false,
				type : "POST",
				url : baseUrl + "/comment/list-" + itemType + "-" + itemId
						+ ".html?type=" + type + "&index=" + index,
				success : function(html) {
					var modWrapper = $('.audio-others-wrapper .audio-comments .mods-wrapper .mod-content-wrapper');

					if (type == 0) {
						modWrapper.find('.all-items').empty().append(html);
					} else if (type == 1) {
						modWrapper.find('.hot-items').empty().append(html);
					} else if (type == 2) {
						modWrapper.find('.authentication-items').empty()
								.append(html);
					} else if (type == 3) {
						modWrapper.find('.attention-items').empty()
								.append(html);
					} else if (type == 4) {
						modWrapper.find('.stranger-items').empty().append(html);
					}

				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					var msg = XMLHttpRequest.statusText;
					var options = {
						type : SELF_ALERT_TYPES.danger,
						msg : msg,
						hasClose : true,
						time : 3000
					};
					selfAlert(options);
				}
			});
}

function getMoreHotComment() {
	$('.audio-others-wrapper .audio-comments .mods-wrapper .mods .hot-tab')
			.trigger('click');
}

/**
 * 删除评论
 * 
 * @param data
 * @param _delete_elem
 */
function deleteComment(data, _delete_elem) {
	$.ajax({
		// async : false,
		type : "POST",
		data : data,
		url : baseUrl + '/comment/cancle',
		success : function(responseData) {
			var _options = {
				type : SELF_ALERT_TYPES.success,
				msg : "评论删除成功",
				hasClose : true,
				time : 3000
			};
			if (responseData.responseCode == '000') {
				selfAlert(_options);
				if (_delete_elem) {
					var _delele_ass = _delete_elem.next('.spit-hr-1');
					if (_delele_ass) {
						_delele_ass.remove();
					}
					var _time = _delete_elem.data('time');
					if (_time) {
						$('#' + _time).remove();
						_RESET_SHOW_DIALOG_MODULE_WRAPPER_FUN_();
					}
					_delete_elem.remove();
				}
			} else {
				_options.msg = responseData.responseMessage;
				_options.type = SELF_ALERT_TYPES.danger, selfAlert(_options);
			}

		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			var msg = XMLHttpRequest.statusText;
			var _options = {
				type : SELF_ALERT_TYPES.danger,
				msg : msg,
				hasClose : true,
				time : 3000
			};
			selfAlert(_options);
		}
	});
}

/**
 * 请求展开对话
 * 
 * @param commentId
 */
function showDialog(commentId) {

	$
			.ajax({
				// async : false,
				type : "POST",
				url : baseUrl + "/comment/dialog/list/" + commentId + ".html",
				success : function(html) {
					$('.show-dialog-module-wrapper').empty().append(html);

					_RESET_SHOW_DIALOG_MODULE_WRAPPER_FUN_();
					layer
							.open({
								type : 1,
								title : [ "查看对话",
										'background-color:rgb(255,255,255);text-align:center;font-weight: 500;' ],
								closeBtn : 0,
								shadeClose : true,
								skin : 'layer-ext-moon',
								content : $('.show-dialog-module-wrapper'),
								area : [ '520px', '450px' ],
								shift : 4,
								scrollbar : false
							});
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					var msg = XMLHttpRequest.statusText;
					var options = {
						type : SELF_ALERT_TYPES.danger,
						msg : msg,
						hasClose : true,
						time : 3000
					};
					selfAlert(options);
				}
			});

}

/**
 * 展示举报模板
 * 
 * @param itemType
 * @param itemId
 */
function showExpose(itemType, itemId) {
	$
			.ajax({
				// async : false,
				type : "POST",
				url : baseUrl + "/expose/template-" + itemType + "-" + itemId
						+ ".html",
				success : function(html) {
					$('.report-model-wrapper').empty().append(html);
					_SHOW_REPORT_WRAPPER_FUN_();
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					var msg = XMLHttpRequest.statusText;
					var options = {
						type : SELF_ALERT_TYPES.danger,
						msg : msg,
						hasClose : true,
						time : 3000
					};
					selfAlert(options);
				}
			});

}

var _REPORT_LAYER_ = null;
/**
 * 弹出举报框
 * 
 * @private
 */
function _SHOW_REPORT_WRAPPER_FUN_() {
	_REPORT_LAYER_ = layer
			.open({
				type : 1,
				title : [ "举 报",
						'background-color:rgb(255,255,255);text-align:center;font-weight: 500;' ],
				closeBtn : 0,
				shadeClose : true,
				skin : 'layer-ext-moon',
				content : $('.report-model-wrapper'),
				area : [ '550px', 'auto' ],
				shift : 4,
				scrollbar : false
			});
}
/**
 * 关闭举报框
 */
function closeExprose() {
	if (_REPORT_LAYER_) {
		layer.close(_REPORT_LAYER_);
	}

}
